package test.serverframe.armc.server.manager.dao.mapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.jdbc.SQL;
import org.ho.yaml.Yaml;

import com.microcore.util.DateUtil;

import test.serverframe.armc.server.manager.controller.ConfigManagerController;
import test.serverframe.armc.server.manager.domain.InvokeRecord;

public class InvokeRecordSqlProvider {

    public String insertSelective(InvokeRecord record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("tb_ssgp_invoke_record");

        if (record.getRecordId() != null) {
            sql.VALUES("record_id", "#{recordId,jdbcType=VARCHAR}");
        }

        if (record.getTaskInvokeId() != null) {
            sql.VALUES("task_invoke_id", "#{taskInvokeId,jdbcType=VARCHAR}");
        }

        if (record.getStartTime() != null) {
            sql.VALUES("start_time", "#{startTime,jdbcType=TIMESTAMP}");
        }

        if (record.getEndTime() != null) {
            sql.VALUES("end_time", "#{endTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskTotal() != null) {
            sql.VALUES("task_total", "#{taskTotal,jdbcType=VARCHAR}");
        }

        if (record.getTaskSuccessTotal() != null) {
            sql.VALUES("task_success_total", "#{taskSuccessTotal,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            sql.VALUES("status", "#{status,jdbcType=BIT}");
        }
        
        if (record.getNodeArr() != null) {
            sql.VALUES("node_arr", "#{nodeArr,jdbcType=VARCHAR}");
        }

        return sql.toString();
    }

    public String updateByPrimaryKeySelective(InvokeRecord record) {
        SQL sql = new SQL();
        sql.UPDATE("tb_ssgp_invoke_record");

        if (record.getTaskInvokeId() != null) {
            sql.SET("task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}");
        }

        if (record.getStartTime() != null) {
            sql.SET("start_time = #{startTime,jdbcType=TIMESTAMP}");
        }

        if (record.getEndTime() != null) {
            sql.SET("end_time = #{endTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskTotal() != null) {
            sql.SET("task_total = #{taskTotal,jdbcType=VARCHAR}");
        }

        if (record.getTaskSuccessTotal() != null) {
            sql.SET("task_success_total = #{taskSuccessTotal,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            sql.SET("status = #{status,jdbcType=BIT}");
        }
        
        if (record.getNodeArr() != null) {
            sql.SET("node_arr = #{nodeArr,jdbcType=VARCHAR}");
        }

        sql.WHERE("record_id = #{recordId,jdbcType=VARCHAR}");

        return sql.toString();
    }

    public String selectByPage(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        Date startTime = null;
        Date endTime = null;
        if (map.get("recordDay") != null) {
            Object day = map.get("recordDay");
            startTime = DateUtil.getDateBefore(new Date(), (Integer) day);
            endTime = new Date();
        }
        sb.append("select record_id, task_invoke_id, start_time, status, node_arr " +
                "from tb_ssgp_invoke_record " +
                "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR} ");
        if (map.get("startTime") != null && map.get("endTime") != null) {
            sb.append(" and start_time>='" + DateUtil.formatDate((Date) map.get("startTime"))
                    + "' AND start_time<='" + DateUtil.formatDate((Date) map.get("endTime")) + "'");
        } else {
        	if (startTime != null && endTime != null) {
                sb.append(" and start_time>='" + DateUtil.formatDate(startTime)
                        + "' AND start_time<='" + DateUtil.formatDate(endTime) + "'");
            }
        }
        Object flag = map.get("flag");
        if (flag!= null) {
            if ((boolean) flag) {
                sb.append(" and date(start_time) = curdate() ");
            } else {
                sb.append(" and DATE_FORMAT( start_time,'%Y-%m-%d') < DATE_FORMAT(NOW(), '%Y-%m-%d')");
            }
        }

        sb.append(" order by start_time desc");
        return sb.toString();
    }
}